/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package test.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import test.hibernate.dao.CustomerDao;
import test.hibernate.pojo.Customer;

/**
 *
 * @author mamat
 */
public class EditCustomerController extends MultiActionController {
    public ModelAndView edit(HttpServletRequest arg0, HttpServletResponse arg1, Customer customer) throws Exception {
        String id = arg0.getParameter("id");        
        CustomerDao customerDao = (CustomerDao) getApplicationContext().getBean("CustomerDao");
        ModelAndView mav = new ModelAndView();
        String action = arg0.getParameter("action")==null?"":arg0.getParameter("action");
        System.out.println(">>>Action :"+action);
        if(action.equalsIgnoreCase("1")){
            customerDao.editCustomer(customer);
            return new ModelAndView("redirect:list.htm");
        } else {
            List<Customer> list = customerDao.getCustomerId(Integer.parseInt(id));
            Customer c = (Customer)list.get(0);

            mav.addObject("customer", c);
            return mav;
        }
    }
}
